package controller;

import DataAcess.Bus;
import Interface.MainGui;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JOptionPane;


/*
 * Class ID	: SE715 
 * Student ID	: 02967
 * Student Name : Vu Ngoc Trung
 * Due Date     : Jul 21, 2013
 * I declare that this assignment is my own work in accordance with FPT Policy.
 */
public class SearchByNameController implements controller.Controller {

    private MainGui form;
    ArrayList<Bus> arrbus;
    controller.Controller controller;
    int mouseSelectedIndex;

    public SearchByNameController(ArrayList arrbus, controller.Controller controller, Interface.MainGui mainGui) {
        this.arrbus = arrbus;
        form = mainGui;
        this.controller = controller;
    }

    @Override
    public void handleEvent(String event) {
        //tranform main controller command that view bus are already chosen
        if (event.equals("View")) {
            ViewBus();
        }
        //tranform main controller command that start search by name
        if (event.equals("Search Name")) {
            SearchName();
        }
    }

    public void view(Bus bus) {
        //show information about bus to interface
        form.lbNameStreet.setText(bus.getName());
        form.lbNoStreet.setText(Integer.toString(bus.getNumber()));
        form.taGoStreet.setText(bus.getGo());
        form.taBackStreet.setText(bus.getBack());
        form.taGoStreet.setEditable(false);
        form.taBackStreet.setEditable(false);
    }

    public void ViewBus() {
        if (form.tbStreet.getValueAt(form.tbStreet.getSelectedRow(), 0) != null) {
            //get location are already chosen
            mouseSelectedIndex = form.tbStreet.getSelectedRow();

            //find this bus in arraylist
            if (!(form.tbStreet.getValueAt(mouseSelectedIndex, 0).equals(""))) {
                Bus bus = null;
                for (Bus bus1 : arrbus) {
                    if (bus1.getNumber() == form.tbStreet.getValueAt(mouseSelectedIndex, 0)) {
                        bus = bus1;
                    }
                }

                view(bus);
                //find the location of street name in bus information using to highlight
                int t = bus.getGo().trim().toLowerCase().indexOf(form.tfSearchStreet.getText().trim().toLowerCase());
                int t2 = bus.getBack().trim().toLowerCase().indexOf(form.tfSearchStreet.getText().trim().toLowerCase());

                form.taGoStreet.requestFocus(true);
                form.taBackStreet.requestFocus(true);
                //highlight the street name
                if (t2 >= 0) {
                    form.taBackStreet.select(t2, t2 + form.tfSearchStreet.getText().trim().length());
                    form.taBackStreet.setSelectedTextColor(Color.blue);
                }

                if (t >= 0) {
                    form.taGoStreet.select(t, t + form.tfSearchStreet.getText().trim().length());
                    form.taGoStreet.setSelectedTextColor(Color.blue);
                }
            }
        }
    }

    public void SearchName() {
        int i = 0;
        //clear table
        for (int j = 0; j < arrbus.size(); j++) {
            for (int k = 0; k < 2; k++) {
                form.tbStreet.setValueAt("", j, k);
            }
        }
        //find and print bus to table
        for (Bus bus : arrbus) {
            String search = form.tfSearchStreet.getText().toLowerCase();
            String busGo = bus.getGo().toLowerCase();
            if (busGo.contains(search) || bus.getBack().contains(search)) {
                form.tbStreet.setValueAt(bus.getNumber(), i, 0);
                form.tbStreet.setValueAt(bus.getName(), i, 1);
                i++;
            }
        }
        //message to user if bus not found!
        if (i == 0) {
            form.taBackStreet.setText("");
            form.taGoStreet.setText("");
            JOptionPane.showMessageDialog(form, "Không tìm thấy xe phù hợp!", "Error", JOptionPane.ERROR_MESSAGE,
                    new javax.swing.ImageIcon(getClass().getResource("/Icon/findnamefail.png")));
        }
    }
}
